﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 

namespace VLUSM_DataAccess.Repositories
{
    public class LopRepository : GenericRepository<MainDataContainer>
    {
        public LopRepository(string connectionString) : base(connectionString, "MainData") { }
        public LopRepository(MainDataContainer context) : base(context) { }

        public Lop GetSingleByID(int IDFilter)
        {
            var query = this.GetFirst<Lop>(d => d.ID == IDFilter);
            return query;
        }

        public Lop GetSingleByMaLop(string MaLopFilter)
        {
            var query = this.GetFirst<Lop>(d => d.MaLop == MaLopFilter);
            return query;
        }

        public Lop GetSingleByTen(string TenFilter)
        {
            var query = this.GetFirst<Lop>(d => d.TenGoi == TenFilter);
            return query;
        }

        public IEnumerable<Lop> GetAll()
        {
            var query = this.GetAll<Lop>();
            return query;
        }

        public IEnumerable<Lop> GetAllByDonViID(int IDFilter)
        {
            var query = this.GetAll<Lop>().Where(l => l.DonViID == IDFilter);
            return query;
        }

        public IEnumerable<Lop> GetAllByKhoaHocID(int IDFilter)
        {
            var query = this.GetAll<Lop>().Where(l => l.KhoaHocID == IDFilter);
            return query;
        }

        public IEnumerable<Lop> GetAllByDonViandKhoaHocID(int DonViIDFilter, int KhoaHocIDFilter)
        {
            var query = this.GetAll<Lop>().Where(l => (l.DonViID == DonViIDFilter) && (l.KhoaHocID == KhoaHocIDFilter));
            return query;
        }


        public void Save(Lop obj)
        {
            try
            {
                if (obj.ID != 0)
                {
                    this.Attach<Lop>(obj);
                }
                else
                {
                    this.Add<Lop>(obj);
                }
            }
            catch { }
        }

        public void Remove(Lop obj)
        {
            this.Delete<Lop>(obj);
        }

        public void Remove(int IDFilter)
        {
            var obj = this.GetSingleOrDefault<Lop>(l => l.ID == IDFilter);
            this.Delete<Lop>(obj);
        }
    }
}
